package com.axeiya.cheddargetter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;

public class Customer {
	
	public static enum FormatType{
		CREATE,
		UPDATE;
	}

	/**
	 * Your code for this customer. Limited to 36 characters. REQUIRED
	 */
	private String code;

	/**
	 * Limited to 20 characters REQUIRED
	 */
	private String firstName;

	/**
	 * Limited to 20 characters REQUIRED
	 */
	private String lastName;

	/**
	 * Valid email address REQUIRED
	 */
	private String email;

	/**
	 * Limited to 60 characters
	 */
	private String company;

	/**
	 * 
	 */
	private boolean isVatExempt;

	/**
	 * If the customer lives in a VAT eligible country and is exempt, provide
	 * the exemption number. Limited to 32 characters
	 */
	private String vatNumber;

	/**
	 * Limited to 255 characters
	 */
	private String notes;

	/**
	 * READ ONLY
	 */

	private Date createdDatetime;

	private Date modifiedDatetime;

	private String gatewayToken;

	/**
	 * TODO: metaData[<user-defined>]
	 */

	/**
	 * The Customer's Subscription REQUIRED
	 */
	private ArrayList<Subscription> subscriptions;



	/**
	 * CREATION CONSTRUCTOR
	 */
	public Customer(String code, String firstName, String lastName, String email,
			Subscription subscription) {
		this.code = code;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.subscriptions = new ArrayList<Subscription>();
		this.subscriptions.add(subscription);
	}

	/**
	 * READ CONSTRUCTOR
	 */
	public Customer(String code, String firstName, String lastName, String email, String company,
			boolean isVatExempt, String vatNumber, String notes, Date createdDatetime,
			Date modifiedDatetime, String gatewayToken, ArrayList<Subscription> subscriptions) {
		super();
		this.code = code;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
		this.company = company;
		this.isVatExempt = isVatExempt;
		this.vatNumber = vatNumber;
		this.notes = notes;
		this.createdDatetime = createdDatetime;
		this.modifiedDatetime = modifiedDatetime;
		this.gatewayToken = gatewayToken;
		this.subscriptions = subscriptions;

	}

	public static Customer parseCustomer(Element customer) {

		String code = customer.getAttributeValue("code");
		String firstName = customer.getChildText("firstName");
		String lastName = customer.getChildText("lastName");
		String email = customer.getChildText("email");
		String company = customer.getChildText("company");
		String gatewayToken = customer.getChildText("gatewayToken");
		boolean isVatExempt = CGTools.parseBoolean(customer.getChildText("isVatExempt"));
		String vatNumber = customer.getChildText("vatNumber");
		Date createdDatetime = CGTools.parseDateTime(customer.getChildText("createdDatetime"));
		Date modifiedDatetime = CGTools.parseDateTime(customer.getChildText("modifiedDatetime"));

		ArrayList<Subscription> subscriptions = new ArrayList<Subscription>();
		;
		try {
			List<Element> subscriptionsElements = XPath.selectNodes(customer,
					"subscriptions/subscription");
			for (Element subscriptionElement : subscriptionsElements) {
				subscriptions.add(Subscription.parseSubscription(subscriptionElement));
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new Customer(code, firstName, lastName, email, company, isVatExempt, vatNumber,
				vatNumber, createdDatetime, modifiedDatetime, gatewayToken, subscriptions);
	}

	public static HashMap<String, String> formatCustomer(Customer customer, FormatType format){
		HashMap<String, String> parameters = new HashMap<String, String>();
		
		if(format == FormatType.CREATE){
			if(customer.getCode() != null && !customer.getCode().isEmpty()){
				parameters.put("code", customer.getCode());
			}else{
				//TODO: throw exception
			}
		}
		
		if(customer.getFirstName() != null){
			parameters.put("firstName", customer.getFirstName());
		}else if(format == FormatType.CREATE){
			//TODO: throw Exception
		}
		
		if(customer.getLastName() != null){
			parameters.put("lastName", customer.getLastName());
		}else if(format == FormatType.CREATE){
			//TODO: throw Exception
		}
		
		if(customer.getEmail() != null){
			parameters.put("email", customer.getEmail());
		}else if(format == FormatType.CREATE){
			//TODO: throw Exception
		}
		
		if(customer.getCompany() != null){
			parameters.put("email", customer.getEmail());
		}
		
		if(customer.getNotes() != null){
			parameters.put("notes", customer.getNotes());
		}
		
		parameters.put("isVatExempt", String.valueOf(CGTools.formatBoolean(customer.getIsVatExempt())));
		
		if(customer.getVatNumber() != null){
			parameters.put("vatNumber", customer.getVatNumber());
		}
		
		return parameters;
	}
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public boolean getIsVatExempt() {
		return isVatExempt;
	}

	public void setIsVatExempt(boolean isVatExempt) {
		this.isVatExempt = isVatExempt;
	}

	public String getVatNumber() {
		return vatNumber;
	}

	public void setVatNumber(String vatNumber) {
		this.vatNumber = vatNumber;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public ArrayList<Subscription> getSubscriptions() {
		return subscriptions;
	}

	public void setSubscriptions(ArrayList<Subscription> subscriptions) {
		this.subscriptions = subscriptions;
	}


	public Date getCreatedDatetime() {
		return createdDatetime;
	}

	public void setCreatedDatetime(Date createdDatetime) {
		this.createdDatetime = createdDatetime;
	}

	public Date getModifiedDatetime() {
		return modifiedDatetime;
	}

	public void setModifiedDatetime(Date modifiedDatetime) {
		this.modifiedDatetime = modifiedDatetime;
	}

	public String getGatewayToken() {
		return gatewayToken;
	}

	public void setGatewayToken(String gatewayToken) {
		this.gatewayToken = gatewayToken;
	}

}
